{
  "name": "Warehouse Stock Discrepancy Resolution Agent",
  "nodes": [
    {
      "parameters": { "updates": ["message"] },
      "id": "tg-trigger-wh",
      "name": "Telegram Trigger",
      "type": "n8n-nodes-base.telegramTrigger",
      "typeVersion": 1.2,
      "position": [0, 0]
    },
    {
      "parameters": {
        "assignments": {
          "assignments": [
            { "name": "user_message", "value": "={{ $json.message.text }}", "type": "string" },
            { "name": "user_name", "value": "={{ $json.message.from.first_name + ' ' + ($json.message.from.last_name || '') }}", "type": "string" },
            { "name": "chat_id", "value": "={{ $json.message.chat.id }}", "type": "string" }
          ]
        }
      },
      "id": "set-wh-context",
      "name": "Workflow Configuration",
      "type": "n8n-nodes-base.set",
      "typeVersion": 3.4,
      "position": [220, 0]
    },
    {
      "parameters": {
        "promptType": "define",
        "text": "=User Name: {{ $json.user_name }}\nUser Message: {{ $json.user_message }}\nChat ID: {{ $json.chat_id }}",
        "options": {
          "systemMessage": "=You are a Warehouse Stock Discrepancy Resolution Assistant.\n\nYour responsibilities:\n1. Identify whether the user wants to REGISTER a new stock discrepancy or CHECK discrepancy status.\n\n2. For new discrepancies, collect:\n   - Warehouse Code\n   - SKU / Item Code\n   - System Stock Quantity\n   - Physical Stock Quantity\n   - Brief description of discrepancy\n\n3. Validate the warehouse and SKU using the Warehouse Stock Master Tool and extract:\n   - Warehouse_Name\n   - Inventory_Manager_Email (for communication)\n   - Warehouse_Status\n\n4. If the warehouse and SKU are valid and active:\n   - Generate a discrepancy reference ID in format:\n     WSD-YYYYMMDD-[last 4 digits of SKU]-HHmmss\n   - Register the discrepancy using the Stock Discrepancy Register Tool\n   - Send acknowledgement email using Gmail Tool to Inventory_Manager_Email\n   - Explain investigation and resolution process\n   - Respond using Telegram Response Tool\n\n5. If warehouse or SKU is invalid:\n   - Politely explain the reason\n   - Do NOT register the discrepancy\n   - Respond using Telegram Response Tool\n\n6. For status checks:\n   - Ask for discrepancy reference ID if missing\n   - Retrieve status using the Stock Discrepancy Status Tool\n   - Explain current investigation stage and next steps\n   - Respond using Telegram Response Tool\n\nStrict rules:\n- Do NOT assign blame.\n- Do NOT promise resolution timelines.\n- Email must be retrieved from master sheet only.\n- ALWAYS respond using the Telegram Response Tool."
        }
      },
      "id": "agent-wh",
      "name": "Warehouse Stock Discrepancy Agent",
      "type": "@n8n/n8n-nodes-langchain.agent",
      "typeVersion": 3,
      "position": [500, 0]
    },
    {
      "parameters": {
        "model": { "mode": "id", "value": "gpt-4o" },
        "options": { "temperature": 0.2 }
      },
      "id": "llm-wh",
      "name": "OpenAI Chat Model",
      "type": "@n8n/n8n-nodes-langchain.lmChatOpenAi",
      "typeVersion": 1.3,
      "position": [320, 220]
    },
    {
      "parameters": {
        "sessionIdType": "customKey",
        "sessionKey": "={{ $('Workflow Configuration').item.json.chat_id }}"
      },
      "id": "memory-wh",
      "name": "Simple Memory",
      "type": "@n8n/n8n-nodes-langchain.memoryBufferWindow",
      "typeVersion": 1.3,
      "position": [500, 220]
    },
    {
      "parameters": { "descriptionType": "manual" },
      "id": "wh-master",
      "name": "Warehouse Stock Master Tool",
      "type": "n8n-nodes-base.googleSheetsTool",
      "typeVersion": 4.7,
      "position": [640, 260]
    },
    {
      "parameters": { "operation": "append", "descriptionType": "manual" },
      "id": "wh-register",
      "name": "Stock Discrepancy Register Tool",
      "type": "n8n-nodes-base.googleSheetsTool",
      "typeVersion": 4.7,
      "position": [800, 260]
    },
    {
      "parameters": { "descriptionType": "manual" },
      "id": "wh-status",
      "name": "Stock Discrepancy Status Tool",
      "type": "n8n-nodes-base.googleSheetsTool",
      "typeVersion": 4.7,
      "position": [960, 320]
    },
    {
      "parameters": {
        "sendTo": "={{ $fromAI('inventory_manager_email') }}",
        "subject": "Warehouse Stock Discrepancy Logged",
        "message": "A stock discrepancy has been logged for your warehouse and is under investigation."
      },
      "id": "gmail-wh",
      "name": "Warehouse Discrepancy Notification Email",
      "type": "n8n-nodes-base.gmailTool",
      "typeVersion": 2.1,
      "position": [1120, 220]
    },
    {
      "parameters": {
        "chatId": "={{ $('Telegram Trigger').item.json.message.chat.id }}",
        "text": "={{ $json.output }}"
      },
      "id": "tg-response-wh",
      "name": "Send Telegram Response",
      "type": "n8n-nodes-base.telegram",
      "typeVersion": 1.2,
      "position": [960, 0]
    }
  ],
  "connections": {
    "Telegram Trigger": { "main": [[{ "node": "Workflow Configuration", "type": "main" }]] },
    "Workflow Configuration": { "main": [[{ "node": "Warehouse Stock Discrepancy Agent", "type": "main" }]] },
    "Warehouse Stock Discrepancy Agent": { "main": [[{ "node": "Send Telegram Response", "type": "main" }]] }
  },
  "active": false
}
